// src/shared/api/product.ts

import { ApiResponse } from '../types';
import {
  getProducts as getProductsService,
  getProductById as getProductByIdService,
  getHotProducts as getHotProductsService,
  getCategories as getCategoriesService,
  createProduct as createProductService,
  updateProduct as updateProductService,
  deleteProduct as deleteProductService,
  getAllProductsForAdmin as getAllProductsForAdminService,
  createCategory as createCategoryService,
  updateCategory as updateCategoryService,
  deleteCategory as deleteCategoryService,
  ProductListQuery,
  ProductListResponse,
  CreateProductRequest,
  UpdateProductRequest,
  CreateCategoryRequest,
  UpdateCategoryRequest
} from '../service/productService';

// ===== 前台商品API（无需权限验证） =====

/**
 * Mock API: 获取商品列表
 * GET /api/products
 */
export const GET_products = async (query: ProductListQuery = {}): Promise<ApiResponse<ProductListResponse>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 300));

    // 调用Service层
    const result = await getProductsService(query);

    return result;
  } catch (error) {
    console.error('Get Products API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 获取商品详情
 * GET /api/products/:id
 */
export const GET_product_detail = async (productId: string): Promise<ApiResponse<{ product: any }>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 200));

    // 调用Service层
    const result = await getProductByIdService(productId);

    return result;
  } catch (error) {
    console.error('Get Product Detail API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 获取热门商品
 * GET /api/products/hot
 */
export const GET_hot_products = async (): Promise<ApiResponse<{ products: any[] }>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 250));

    // 调用Service层
    const result = await getHotProductsService();

    return result;
  } catch (error) {
    console.error('Get Hot Products API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 获取分类列表
 * GET /api/categories
 */
export const GET_categories = async (): Promise<ApiResponse<{ categories: any[] }>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 150));

    // 调用Service层
    const result = await getCategoriesService();

    return result;
  } catch (error) {
    console.error('Get Categories API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

// ===== 管理员商品管理API =====

/**
 * Mock API: 获取所有商品（管理员功能）
 * GET /api/admin/products
 */
export const GET_admin_products = async (query: ProductListQuery = {}): Promise<ApiResponse<ProductListResponse>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 400));

    // 调用Service层
    const result = await getAllProductsForAdminService(query);

    return result;
  } catch (error) {
    console.error('Get Admin Products API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 添加商品（管理员功能）
 * POST /api/admin/products
 */
export const POST_admin_product = async (body: CreateProductRequest): Promise<ApiResponse<{ product: any }>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 600));

    // 调用Service层
    const result = await createProductService(body);

    return result;
  } catch (error) {
    console.error('Create Product API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 更新商品（管理员功能）
 * PUT /api/admin/products/:id
 */
export const PUT_admin_product = async (productId: string, body: UpdateProductRequest): Promise<ApiResponse<{ product: any }>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 500));

    // 调用Service层
    const result = await updateProductService(productId, body);

    return result;
  } catch (error) {
    console.error('Update Product API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 删除商品（管理员功能）
 * DELETE /api/admin/products/:id
 */
export const DELETE_admin_product = async (productId: string): Promise<ApiResponse<null>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 400));

    // 调用Service层
    const result = await deleteProductService(productId);

    return result;
  } catch (error) {
    console.error('Delete Product API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

// ===== 管理员分类管理API =====

/**
 * Mock API: 添加分类（管理员功能）
 * POST /api/admin/categories
 */
export const POST_admin_category = async (body: CreateCategoryRequest): Promise<ApiResponse<{ category: any }>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 400));

    // 调用Service层
    const result = await createCategoryService(body);

    return result;
  } catch (error) {
    console.error('Create Category API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 更新分类（管理员功能）
 * PUT /api/admin/categories/:id
 */
export const PUT_admin_category = async (categoryId: string, body: UpdateCategoryRequest): Promise<ApiResponse<{ category: any }>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 400));

    // 调用Service层
    const result = await updateCategoryService(categoryId, body);

    return result;
  } catch (error) {
    console.error('Update Category API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 删除分类（管理员功能）
 * DELETE /api/admin/categories/:id
 */
export const DELETE_admin_category = async (categoryId: string): Promise<ApiResponse<null>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 350));

    // 调用Service层
    const result = await deleteCategoryService(categoryId);

    return result;
  } catch (error) {
    console.error('Delete Category API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};